<?php

namespace App\Admin\Controllers\kly;

use App\Models\kly\Kly_notice;
use App\Models\kly\Kly_record;
use App\Models\Student;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;
use Encore\Admin\Controllers\HasResourceActions;

class Kly_recordController  extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('admin.test_score_record'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());  
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('admin.test_score_record'));
            $content->description(trans('admin.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.test_score_record'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Kly_record::class, function (Grid $grid) {
            $notice_id = request('notice_id');
            $notice = Kly_notice::where('id',$notice_id)->toArray();

            $grid->model()->where('notice_id',$notice_id);
            $grid->id('序号')->sortable();
            $grid->student_id(trans('admin.en_name'))->display(function(){
                return Student::find($this->student_id)->en_name;
            });
            if($notice['type'] == '1'){
                if($notice['tag'] == 'kaoqin'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '正常';
                        }elseif($this->status == '2'){
                            return '请假';
                        }elseif($this->status == '3'){
                            return '迟到';
                        }elseif($this->status == '4'){
                            return '缺勤';
                        }elseif($this->status == '5'){
                            return '家长代课';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'test'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '正常';
                        }elseif($this->status == '2'){
                            return '未参加';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'jiaxiao'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '准交';
                        }elseif($this->status == '2'){
                            return '未交';
                        }elseif($this->status == '3'){
                            return '补交';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'dictation'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '正常';
                        }elseif($this->status == '2'){
                            return '未参加';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'zuoye'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '准交';
                        }elseif($this->status == '2'){
                            return '未交';
                        }elseif($this->status == '3'){
                            return '补交';
                        }else{
                            return '';
                        }
                    });
                }else{
                    $arr = DB::table('kly_setting')->where('id',$notice['setting_id'])->where('tag',$notice['tag'])->get(['status_item']);
                    $arr = json_decode(json_encode($arr),true);
                    $grid->status('状态')->display(function() use ($arr){
                        for($i=0;$i<count($arr);$i++){
                            $j = (string)($i+1);
                            if($this->status == $j){
                                return $arr[$i];
                            }
                        }
                    });
                }
            }elseif($notice['type'] == '2'){
                $grid->score('分数');
            }elseif($notice['type'] == '3'){
                if($notice['tag'] == 'kaoqin'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '正常';
                        }elseif($this->status == '2'){
                            return '请假';
                        }elseif($this->status == '3'){
                            return '迟到';
                        }elseif($this->status == '4'){
                            return '缺勤';
                        }elseif($this->status == '5'){
                            return '家长代课';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'test'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '正常';
                        }elseif($this->status == '2'){
                            return '未参加';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'jiaxiao'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '准交';
                        }elseif($this->status == '2'){
                            return '未交';
                        }elseif($this->status == '3'){
                            return '补交';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'dictation'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '正常';
                        }elseif($this->status == '2'){
                            return '未参加';
                        }else{
                            return '';
                        }
                    });
                }elseif($notice['tag'] == 'zuoye'){
                    $grid->status('状态')->display(function(){
                        if($this->status == '1'){
                            return '准交';
                        }elseif($this->status == '2'){
                            return '未交';
                        }elseif($this->status == '3'){
                            return '补交';
                        }else{
                            return '';
                        }
                    });
                }else{
                    $arr = DB::table('kly_setting')->where('id',$notice['setting_id'])->where('tag',$notice['tag'])->get(['status_item']);
                    $arr = json_decode(json_encode($arr),true);
                    $grid->status('状态')->display(function() use ($arr){
                        for($i=0;$i<count($arr);$i++){
                            $j = (string)($i+1);
                            if($this->status == $j){
                                return $arr[$i];
                            }
                        }
                    });
                }
                $grid->score('分数');
            }

            
            $grid->desc('备注');
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();
            });
            $grid->disableCreateButton();
            //$grid->disableActions();
            //禁用导出按钮
            //$grid->disableExport();
            $filename="成绩记录";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            $grid->disableFilter();//禁用查询
        });
    }

    protected function form()
    {
        $form = new Form(new Kly_record());
        $form->display('id', 'ID');
        $form->radio('status')->options([
            1 => '正常',
        ]);
        //$form->select('attendance_status', trans('admin.attendance_status'))->options([1=>'正常',2=>'请假',3=>'迟到',4=>'缺勤'])->rules('required')->addElementLabelClass('must-input');
        $form->textarea('desc','描述');
        $form->number('score','分数');


        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        // $form->saved(function (Form $form) {
        //     $attendance_report_id = $form->model()->attendance_report_id;
        //     // 跳转页面
        //     return redirect('/admin/attendance_report/'.$attendance_report_id);
        //     });
        return $form;
    }
}
